package com.reeygu.item.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 处理秒杀业务的队列配置
 */
@Configuration
public class RabbitMQConfig {
    private static final String QUEUE_GOODS = "seckillQueue";
    private static final String EXCHANGE = "seckillExchange";


    @Bean
    public RabbitTemplate rabbitTemplate() {
        return new RabbitTemplate();
    }

    @Bean
    public Queue queueGoods() {
        return QueueBuilder.durable(QUEUE_GOODS).build();
    }

    @Bean
    public TopicExchange exchange() {
        return new TopicExchange(EXCHANGE);
    }

    @Bean
    public Binding queueGoodsBindingExchange(@Qualifier("queueGoods") Queue queue,
                                             @Qualifier("exchange") TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("seckill.#");
    }
}
